package com.bawei.lib_common.diffutil

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bawei.lib_common.R
import com.bawei.lib_common.appContext
import com.bawei.lib_image.GlideApp

/**
 *作者：lxy
 *功能:
 *时间：2025/5/30
 */
class MyAdapter : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {


    private val list: MutableList<MyModel> = mutableListOf()

    fun setData(newData:MutableList<MyModel>){
        val diffResult = DiffUtil.calculateDiff(DiffCallBack(list.toList(), newData))
        list.clear()
        list.addAll(newData)
        diffResult.dispatchUpdatesTo(this)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_dif, parent, false)
        return MyViewHolder(view)
    }


    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.tv.text = list[position].title
        GlideApp.with(holder.itemView.context).load(list[position].url).into(holder.iv)
    }


    inner class MyViewHolder(itemView: View) : ViewHolder(itemView) {
        val iv: ImageView
        val tv: TextView

        init {
            iv = itemView.findViewById(R.id.iv)
            tv = itemView.findViewById(R.id.tv)
        }
    }


}